package cn.cxy.customviews.misc

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class Taiji @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {

    private var whitePaint: Paint? = null   //白色画笔
    private var blackPaint: Paint? = null   //黑色画笔
    private var degrees = 0f

    //初始化画笔函数
    private fun initPaints() {
        whitePaint = Paint()
        whitePaint!!.isAntiAlias = true
        whitePaint!!.color = Color.WHITE

        blackPaint = Paint(whitePaint)
        blackPaint!!.color = Color.BLACK
    }

    init {
        initPaints()
    }

    override fun onDraw(canvas: Canvas) {
        val width = width                          //画布宽度   view设置的宽度
        val height = height                        //画布高度     view设置的高度

        canvas.translate((width / 2).toFloat(), (height / 2).toFloat())                     //将画布移动到中心
        canvas.drawColor(Color.GRAY)                           //绘制背景色

        //在绘制完背景色后调用即可
        canvas.rotate(degrees)

        //半径
        val radius = Math.min(width, height) / 2 - 100

        val rf = RectF((-radius).toFloat(), (-radius).toFloat(), radius.toFloat(), radius.toFloat())

        canvas.drawArc(rf, -90f, 180f, true, whitePaint!!)
        canvas.drawArc(rf, 90f, 180f, true, blackPaint!!)


        //绘制两个小圆
        val smallRadius = radius / 2    //小圆半径为大圆的一般
        canvas.drawCircle(0f, (-smallRadius).toFloat(), smallRadius.toFloat(), blackPaint!!)
        canvas.drawCircle(0f, smallRadius.toFloat(), smallRadius.toFloat(), whitePaint!!)

        //太极的小圆
        val smallCircleRadius = smallRadius / 4    //小圆半径为大圆的一般
        canvas.drawCircle(0f, (-smallRadius).toFloat(), smallCircleRadius.toFloat(), whitePaint!!)
        canvas.drawCircle(0f, smallRadius.toFloat(), smallCircleRadius.toFloat(), blackPaint!!)
    }
}